package com.weiaibenpao.demo.chislim.sport;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.hurui.activity.HBaseActivity;
import com.weiaibenpao.demo.chislim.sport.adapter.RecordAdapter;
import com.weiaibenpao.demo.chislim.sport.bean.RegionDataBean;
import com.weiaibenpao.demo.chislim.sport.bean.RegionRecordBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.weiaibenpao.demo.chislim.sport.utils.Constant.SPORT_TYPE;

public class RegionRecordActivity extends HBaseActivity {



    @BindView(R.id.backImg)
    ImageView backImg;
    @BindView(R.id.record_recycle)
    RecyclerView recordRecycle;
    @BindView(R.id.record_tv)
    TextView record_tv;
    private RecordAdapter recordAdapter;
    private List<String> list;
    private int type;
    private UserBean userBean;
    private String selectName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_region_recrod);

        ButterKnife.bind(this);
        userBean = UserBean.getUserBean();
        type = getIntent().getIntExtra(SPORT_TYPE,2);
        Log.e("wlx22", "onResponse: "+type);
        initAdapter();
        initData();

    }

    private void initData() {
        //TODO 初始化数据
        list=new ArrayList<>();
        apiStores.getselectRecord(userBean.userId,type).enqueue(new Callback<RegionRecordBean>() {
            @Override
            public void onResponse(Call<RegionRecordBean> call, Response<RegionRecordBean> response) {
                Log.e("wlx22", "RegionRecordBean: "+new Gson().toJson(response));
                    if ( response.body().getCode()==0&&response.body().getData().getExtra().getTotalSize()!=0){
                        recordAdapter.refresh(response.body().getData().getXingzhengSportlist());
                        record_tv.setVisibility(View.GONE);
                    }else
                        record_tv.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFailure(Call<RegionRecordBean> call, Throwable throwable) {
                Log.e("wlc22", "onFailure: "+throwable.getMessage() );
            }
        });

    }

    private void initAdapter() {
        //TODO 初始化adapter
        recordAdapter = new RecordAdapter(this);
        recordRecycle.setLayoutManager(new LinearLayoutManager(this));
        recordRecycle.setAdapter( recordAdapter);

        recordAdapter.setOnItemClickListener(new RecordAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(RegionDataBean bean) {
               getSelectName( bean);
                if (type==2){
                    SportHomeDistrictActivity.openActivity(RegionRecordActivity.this,bean,selectName);
                }else if (type==3){
                    SportOutDistrictActivity.openActivity(RegionRecordActivity.this,bean,selectName);
                }
                EventBus.getDefault().post(true);
                finish();

            }
        });
    }

    private void getSelectName(RegionDataBean bean) {
        if (bean.getCity().equals(" ")){

            selectName=bean.getProvince();

        }else if (bean.getArea().equals(" ")){

            selectName=bean.getCity();

        }else{
            selectName=bean.getArea();


        }
    }

    public static void openActivity(Context context,int type){
        Intent intent = new Intent(context, RegionRecordActivity.class);
        intent.putExtra(SPORT_TYPE,type);
        context.startActivity(intent);
    }

    @OnClick(R.id.backImg)
    public void onClick() {
        finish();
    }
}
